package com.projectsexception.festivales.util;

import java.util.Comparator;

import com.projectsexception.festivales.setup.ShowInfo;

public class ShowInfoComparator implements Comparator<ShowInfo> {
    
    private static final int LAST_SHOW = 8;
    private static final int HOURS_IN_DAY = 24;

    @Override
    public int compare(ShowInfo showInfo1, ShowInfo showInfo2) {
        int compare = showInfo1.getDate().compareTo(showInfo2.getDate());
        if (compare == 0) {
            compare = compareTimes(showInfo1.getStartTime(), showInfo2.getStartTime());
        }
        return compare;
    }
    
    public static int compareTimes(String t1, String t2) {
        int compare = 0;
        String[] time1 = t1.split(":");
        String[] time2 = t2.split(":");
        if (time1.length == 2 && time2.length == 2) {
            int hour1 = Integer.parseInt(time1[0]);
            if (hour1 < LAST_SHOW) {
                hour1 += HOURS_IN_DAY;
            }
            int hour2 = Integer.parseInt(time2[0]);                
            if (hour2 < LAST_SHOW) {
                hour2 += HOURS_IN_DAY;
            }
            compare = hour1 - hour2;
            if (compare == 0) {
                compare = Integer.parseInt(time1[1]) - Integer.parseInt(time2[1]);
            }
        }
        return compare;
    }
    
    public static int minutes(String start, String end) {
        int compare = 0;
        String[] startA = start.split(":");
        String[] endA = end.split(":");
        if (startA.length == 2 && endA.length == 2) {
            int startHour = Integer.parseInt(startA[0]);
            if (startHour < LAST_SHOW) {
                startHour += HOURS_IN_DAY;
            }
            int endHour = Integer.parseInt(endA[0]);                
            if (endHour < LAST_SHOW) {
                endHour += HOURS_IN_DAY;
            }
            compare = (endHour - startHour) * 60;
            compare += Integer.parseInt(endA[1]) - Integer.parseInt(startA[1]);
        }
        return compare;
    }

}
